package org.test.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by Hy on 2018/7/6.
 * JDK动态代理增强
 */
public class EnDynamicProxy implements InvocationHandler{
    private Object target;

    public EnDynamicProxy(Object target) {
        this.target = target;
    }
    private void before(){
        System.out.println("Before");
    }
    private void after(){
        System.out.println("After");
    }
    public <T> T getProxy(){
        return (T) Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                this
                );
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object result = method.invoke(target, args);
        after();
        return result;
    }

    public static void main(String[] args) {
        EnDynamicProxy enDynamicProxy=new EnDynamicProxy(new HelloImpl());
        Hello proxy = enDynamicProxy.getProxy();
        proxy.say("Jack");
    }
}
